
local yichou = fk.CreateSkill {
  name = "hanqing__yichou&",
}

Fk:loadTranslationTable{
  ["hanqing__yichou&"] = "夷雠",
  [":hanqing__yichou&"] = "若你场上的牌点数和为全场最大，你可以将【影】当【杀】使用，且以此法使用的【杀】仅能如下响应："..
  "将手牌调整为偶数并将半数非【影】手牌替换为【影】视为使用【闪】。",
}

local U = require "packages/utility/utility"

local function yichouCount(player)
  local n = 0
  for _, id in ipairs(player:getCardIds("ej")) do
    n = n + Fk:getCardById(id).number
  end
  return n
end

yichou:addEffect("viewas", {
  mute = true,
  mute_card = false,
  pattern = "slash,jink",
  prompt = function (self, player, selected_cards, selected)
    if #player:getViewAsCardNames("hanqing__yichou", {"slash"}) > 0 then
      return "#hanqing__yichou-slash"
    elseif #player:getViewAsCardNames("hanqing__yichou", {"jink"}) > 0 then
      local n1 = (player:getHandcardNum() + 1) // 2
      local n2 = player:getHandcardNum() // 2
      return "#hanqing__yichou-jink:::"..n1..":"..n2
    end
  end,
  handly_pile = true,
  card_filter = function(self, player, to_select, selected)
    if #player:getViewAsCardNames("hanqing__yichou", {"slash"}) > 0 then
      return #selected == 0 and Fk:getCardById(to_select).trueName == "shade"
    elseif #player:getViewAsCardNames("hanqing__yichou", {"jink"}) > 0 then
      return table.contains(player:getCardIds("h"), to_select) and
        Fk:getCardById(to_select).trueName ~= "shade" and #selected * 2 < player:getHandcardNum()
    end
  end,
  view_as = function(self, player, cards)
    if #cards == 0 then return end
    if Fk:getCardById(cards[1]).trueName == "shade" then
      if #cards ~= 1 then return end
      local card = Fk:cloneCard("slash")
      card.skillName = "hanqing__yichou"
      card:addSubcard(cards[1])
      return card
    else
      if #cards * 2 < player:getHandcardNum() then return end
      local card = Fk:cloneCard("jink")
      card.skillName = "hanqing__yichou"
      card:addFakeSubcards(cards)
      return card
    end
  end,
  before_use = function (self, player, use)
    local room = player.room
    player:broadcastSkillInvoke(yichou.name)
    if use.card.trueName == "jink" then
      room:notifySkillInvoked(player, yichou.name, "defensive")
      local shade = U.getShade(room, player:getHandcardNum() // 2)
      local moves = {}
      table.insert(moves, {
        ids = use.card.fake_subcards,
        from = player,
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonExchange,
        skillName = yichou.name,
        proposer = player,
        moveVisible = true,
      })
      table.insert(moves, {
        ids = shade,
        to = player,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonExchange,
        skillName = yichou.name,
        proposer = player,
        moveVisible = true,
      })
      room:moveCards(table.unpack(moves))
    else
      room:notifySkillInvoked(player, yichou.name, "offensive")
    end
  end,
  enabled_at_play = function (self, player)
    return #player:getViewAsCardNames("hanqing__yichou", {"slash"}) > 0 and
      table.every(Fk:currentRoom().alive_players, function (p)
        return yichouCount(player) >= yichouCount(p)
      end)
  end,
  enabled_at_response = function (self, player, response)
    if response then return end
    if #player:getViewAsCardNames("hanqing__yichou", {"slash"}) > 0 then
      return table.every(Fk:currentRoom().alive_players, function (p)
        return yichouCount(player) >= yichouCount(p)
      end)
    elseif #player:getViewAsCardNames("hanqing__yichou", {"jink"}) > 0 and
      Fk:currentRoom():getBanner("hanqing__yichou") then
      local cards = table.filter(player:getCardIds("h"), function (id)
        return Fk:getCardById(id).trueName ~= "shade"
      end)
      return #cards * 2 >= player:getHandcardNum()
    end
  end,
})

yichou:addAI(nil, "vs_skill")

return yichou
